package cn.modificator.relationtree.mainlist

import android.content.Context
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import cn.modificator.relationtree.R
import cn.modificator.relationtree.utils.DeviceUtil
import org.jetbrains.anko.*

/**
 * Created by Modificator
 * time: 2017/9/18.下午9:00
 * des:create file and achieve model
 */

class MainItemCell(context: Context) : FrameLayout(context) {
    var tvName = TextView(context)
    var tvCarType = TextView(context)
    var tvPhoneNum = TextView(context)
    var btnCall = ImageView(context)

    init {
        tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20.toFloat())
        tvName.setTextColor(0xff000000.toInt())
        tvCarType.setTextColor(0xff000000.toInt())
        tvCarType.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11.toFloat())
        btnCall.imageResource = R.drawable.ic_call
        btnCall.setPadding(DeviceUtil.dp2Px(7.toFloat()), DeviceUtil.dp2Px(7.toFloat()), DeviceUtil.dp2Px(7.toFloat()), DeviceUtil.dp2Px(7.toFloat()))
        btnCall.rotation=-105.toFloat()
        btnCall.scaleType=ImageView.ScaleType.CENTER_INSIDE
        btnCall.isClickable=true
        addView(tvName)
        addView(tvCarType)
        addView(tvPhoneNum)
        addView(btnCall)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        var heigth = b - t;
        var width = r - l;

        tvName.layout(DeviceUtil.dp2Px(15.toFloat()), DeviceUtil.dp2Px(7.toFloat()), tvName.measuredWidth + DeviceUtil.dp2Px(15.toFloat()), tvName.measuredHeight + DeviceUtil.dp2Px(7.toFloat()))
        tvCarType.layout(tvName.measuredWidth + DeviceUtil.dp2Px(22.toFloat()), tvName.measuredHeight - tvCarType.measuredHeight + DeviceUtil.dp2Px(3.toFloat()),
                tvName.measuredWidth + tvCarType.measuredWidth + DeviceUtil.dp2Px(25.toFloat()), tvName.measuredHeight + DeviceUtil.dp2Px(7.toFloat()))
        tvPhoneNum.layout(DeviceUtil.dp2Px(15.toFloat()), tvName.measuredHeight + DeviceUtil.dp2Px(7.toFloat()), tvPhoneNum.measuredWidth, tvName.measuredHeight + tvPhoneNum.measuredHeight + DeviceUtil.dp2Px(7.toFloat()))
        btnCall.layout(width - btnCall.measuredWidth-DeviceUtil.dp2Px(15F), (heigth - btnCall.measuredWidth) / 2, width-DeviceUtil.dp2Px(15F), heigth - ((heigth - btnCall.measuredWidth) / 2))
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = View.MeasureSpec.getSize(widthMeasureSpec)

        tvName.measure(MeasureSpec.makeMeasureSpec(width - DeviceUtil.dp2Px(0.toFloat()), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(DeviceUtil.dp2Px(28.toFloat()), MeasureSpec.EXACTLY))
        tvCarType.measure(MeasureSpec.makeMeasureSpec(width - DeviceUtil.dp2Px(0.toFloat()), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(DeviceUtil.dp2Px(15.toFloat()), MeasureSpec.EXACTLY))
        tvPhoneNum.measure(MeasureSpec.makeMeasureSpec(width - DeviceUtil.dp2Px(0.toFloat()), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(DeviceUtil.dp2Px(22.toFloat()), MeasureSpec.EXACTLY))
        btnCall.measure(MeasureSpec.makeMeasureSpec(width - DeviceUtil.dp2Px(0.toFloat()), MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(DeviceUtil.dp2Px(0.toFloat()), MeasureSpec.EXACTLY))
        setMeasuredDimension(width, tvName.measuredHeight + tvPhoneNum.measuredHeight + DeviceUtil.dp2Px(10.toFloat()))
    }
}
